#!/bin/bash
# Git Hooks 动态配置系统 - 系统检查脚本

set -e

echo "=========================================="
echo "Git Hooks 动态配置系统 - 系统检查"
echo "=========================================="

# 检查 Python 是否可用
echo "检查 Python..."
if command -v python3 &> /dev/null; then
    python3 --version
    echo "[成功] Python3 可用"
    PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
    python --version
    echo "[成功] Python 可用"
    PYTHON_CMD="python"
else
    echo "[错误] Python 未安装或不在 PATH 中"
    echo "请安装 Python 3.7 或更高版本"
    exit 1
fi

# 检查 Git 是否可用
echo ""
echo "检查 Git..."
if command -v git &> /dev/null; then
    git --version
    echo "[成功] Git 可用"
else
    echo "[错误] Git 未安装或不在 PATH 中"
    echo "请安装 Git"
    exit 1
fi

# 检查 PyYAML 依赖
echo ""
echo "检查 PyYAML 依赖..."
if $PYTHON_CMD -c "import yaml; print('PyYAML 版本:', yaml.__version__)" 2>/dev/null; then
    echo "[成功] PyYAML 已安装"
else
    echo "[警告] PyYAML 未安装"
    echo "正在尝试安装..."
    if $PYTHON_CMD -m pip install PyYAML; then
        echo "[成功] PyYAML 安装完成"
    else
        echo "[错误] 无法安装 PyYAML，请手动安装: pip install PyYAML"
        exit 1
    fi
fi

# 检查项目文件结构
echo ""
echo "检查项目文件结构..."
missing_files=""

files_to_check=(
    "config/hooks.yaml"
    "core/config_parser.py"
    "core/task_executor.py"
    "core/hook_manager.py"
    "tools/install.py"
    "tools/config.py"
)

for file in "${files_to_check[@]}"; do
    if [ ! -f "$file" ]; then
        missing_files="$missing_files $file"
    fi
done

if [ -n "$missing_files" ]; then
    echo "[错误] 缺少文件:$missing_files"
    exit 1
else
    echo "[成功] 所有核心文件都存在"
fi

# 运行简单测试
echo ""
echo "运行系统测试..."
if $PYTHON_CMD simple_test.py; then
    echo "[成功] 系统测试通过"
else
    echo "[错误] 系统测试失败"
    exit 1
fi

echo ""
echo "=========================================="
echo "系统检查完成 - 所有检查都通过！"
echo "=========================================="
echo ""
echo "下一步操作:"
echo "1. 运行安装脚本: $PYTHON_CMD tools/install.py --install"
echo "2. 查看配置: $PYTHON_CMD tools/config.py show"
echo "3. 验证配置: $PYTHON_CMD tools/config.py validate"
echo "4. 查看文档: cat docs/README.md"
echo ""
